<?php
    class VentePriveeDetail {
        //Alias sur table : vpd
        public $id_vente_privee_detail;
        public $vente_privee;
        public $produit;
        public $quantite_restante;
        public $promotion;

        //Constructeur complet
        //(VentePrivee)vente_privee, (Produit)produit
        public function __construct($id_vente_privee_detail, $vente_privee, $produit, $quantite_restante, $promotion) {
            $this->setId($id_vente_privee_detail);
            $this->setVentePrivee($vente_privee);
            $this->setProduit($produit);
            $this->setQuantiteRestante($quantite_restante);
            $this->setPromotion($promotion);
        }
        
        //Ajoute un objet VentePriveeDetail à la BDD
        //Paramètre : aucun
        //Retourne (VentePriveeDetail)
        public function add() {
            $details = $this->vente_privee->getVentePriveeDetails();
            $temp = null;
            foreach($details as $d) {
                if($d->getProduit() == $this->produit) {
                    $temp = $d;
                    $d->setQuantiteRestante($d->getQuantiteRestante() + $this->quantite_restante);
                    $d->update();
                    return $d;
                }
            }
            $sql =  'INSERT INTO vente_privee_detail (id_vente_privee,id_produit,quantite_restante,promotion) VALUES
                    ('.intval($this->vente_privee->getId()).','.intval($this->produit->getId()).','
                    .intval($this->quantite_restante).', '.intval($this->promotion).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
        
        //Modifie une VentePriveeDetail dans la BDD
        //Paramètre : aucun
        //Retourne (VentePriveeDetail)
        public function update(){
            $sql = 'UPDATE vente_privee_detail
                    SET 
                        id_vente_privee = '.intval($this->vente_privee->getId()).',
                        id_produit = '.intval($this->produit->getId()).',
                        quantite_restante = '.intval($this->quantite_restante).',
                        promotion = '.intval($this->promotion).'
                    WHERE
                        id_vente_privee_detail = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
        
        //Permet de remettre à 0 la quantté
        //Paramètre : aucun
        //Retourne rien 
        public function suppr(){
              $this->setQuantiteRestante(0);
              $this->update();
        }
        
       
        //Récupère le prix actuel de cet article pour cette vente privée
        //Paramètre : OPTIONNEl (Booléen)ttc
        //Retourne (double)
        public function getPrixActuel($ttc = true) {
            if($ttc) {
                $temp = ($this->produit->getPrixUnit()*(1 + $this->produit->getTva()->getTaux()/100)) * (1 - $this->promotion/100);
            }
            else {
                $temp = ($this->produit->getPrixUnit()) * (1 - $this->promotion/100);
            }
            
            return number_format($temp, 2);
        }
        
        //Récupère un objet VentePriveeDetail grâce à son ID
        //Paramètre : (int)id
        //Retourne(VentePriveeDetail)
        public static function rechercheVentePriveeDetail($id) {
            $sql =  'SELECT
                        vp.id_vente_privee as vpid_vente_privee, vp.libelle as vplibelle, vp.date_debut as vpdate_debut, 
                        vp.date_fin as vpdate_fin, 
                        vpd.id_vente_privee_detail as vpdid_vente_privee_detail, vpd.quantite_restante as vpdquantite_restante, 
                        vpd.promotion as vpdpromotion,
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock
                    FROM vente_privee_detail vpd
                    LEFT JOIN vente_privee vp ON vpd.id_vente_privee = vp.id_vente_privee
                    LEFT JOIN produit prod ON vpd.id_produit = prod.id_produit
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        vpd.id_vente_privee_detail = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                                        $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $produit = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                    $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], 
                                    $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
                $vente_privee = new VentePrivee($p['vpid_vente_privee'], $p['vplibelle'], $p['vpdate_debut'], $p['vpdate_fin']);
                return new VentePriveeDetail($p['vpdid_vente_privee_detail'], $vente_privee, $produit, $p['vpdquantite_restante'], 
                                                    $p['vpdpromotion']);
            }
        }
        
        //GETTERS ET SETTERS
        public function getId() {return $this->id_vente_privee_detail;}
        public function getVentePrivee() {return $this->vente_privee;}
        public function getProduit() {return $this->produit;}
        public function getQuantiteRestante() {return $this->quantite_restante;}
        public function getPromotion() {return $this->promotion;}

        public function setId($temp) {$this->id_vente_privee_detail = intval($temp);}
        public function setVentePrivee($temp) {$this->vente_privee = $temp;}
        public function setProduit($temp) {$this->produit = $temp;}
        public function setQuantiteRestante($temp) {$this->quantite_restante = intval($temp);}
        public function setPromotion($temp) {$this->promotion = intval($temp);}
    }

?>
